﻿

Namespace RSS.ImportExport


    Friend Class DataTable

        Private mFinanceHelper As New RssHelper

        ''' <summary>
        ''' Converts a list of feed items to a System.Data.DataTable
        ''' </summary>
        ''' <param name="items">The list of feed items</param>
        ''' <returns>The converted System.Data.DataTable containing the quote informations</returns>
        ''' <remarks></remarks>
        Public Function FromQuotesBaseData(ByVal items As IEnumerable(Of FeedItem)) As System.Data.DataTable
            If items IsNot Nothing Then
                Dim dt As New System.Data.DataTable
                dt.Columns.Add(RssHelper.NameFeedAuthor, GetType(String))
                dt.Columns.Add(RssHelper.NameFeedCategory_Domain, GetType(Uri))
                dt.Columns.Add(RssHelper.NameFeedCategory_Name, GetType(String))
                dt.Columns.Add(RssHelper.NameFeedComments, GetType(Uri))
                dt.Columns.Add(RssHelper.NameFeedDescription, GetType(String))
                dt.Columns.Add(RssHelper.NameFeedEnclosure_Length, GetType(Long))
                dt.Columns.Add(RssHelper.NameFeedEnclosure_Type, GetType(String))
                dt.Columns.Add(RssHelper.NameFeedEnclosure_Url, GetType(Uri))
                dt.Columns.Add(RssHelper.NameFeedGuid, GetType(String))
                dt.Columns.Add(RssHelper.NameFeedGuid_PermaLink, GetType(Boolean))
                dt.Columns.Add(RssHelper.NameFeedInsertDate, GetType(Date))
                dt.Columns.Add(RssHelper.NameFeedLink, GetType(Uri))
                dt.Columns.Add(RssHelper.NameFeedPubDate, GetType(Date))
                dt.Columns.Add(RssHelper.NameFeedSource_Title, GetType(String))
                dt.Columns.Add(RssHelper.NameFeedSource_Url, GetType(Uri))
                dt.Columns.Add(RssHelper.NameFeedTitle, GetType(String))

                For Each item In items
                    Dim row As System.Data.DataRow = dt.NewRow
                    row(RssHelper.NameFeedAuthor) = item.Author
                    If item.Category IsNot Nothing Then
                        row(RssHelper.NameFeedCategory_Domain) = item.Category.Domain
                        row(RssHelper.NameFeedCategory_Name) = item.Category.Name
                    End If
                    row(RssHelper.NameFeedComments) = item.Comments
                    row(RssHelper.NameFeedDescription) = item.Description
                    If item.Enclosure IsNot Nothing Then
                        row(RssHelper.NameFeedEnclosure_Length) = item.Enclosure.Length
                        row(RssHelper.NameFeedEnclosure_Type) = item.Enclosure.Type
                        row(RssHelper.NameFeedEnclosure_Url) = item.Enclosure.Url
                    End If
                    If item.GUID IsNot Nothing Then
                        row(RssHelper.NameFeedGuid) = item.GUID.ID
                        row(RssHelper.NameFeedGuid_PermaLink) = item.GUID.IsPermaLink
                    End If
                    row(RssHelper.NameFeedInsertDate) = item.InsertDate
                    row(RssHelper.NameFeedLink) = item.Link
                    row(RssHelper.NameFeedPubDate) = item.PublishDate
                    If item.Source IsNot Nothing Then
                        row(RssHelper.NameFeedSource_Title) = item.Source.Title
                        row(RssHelper.NameFeedSource_Url) = item.Source.Url
                    End If
                    row(RssHelper.NameFeedTitle) = item.Title
                    dt.Rows.Add(row)
                Next
                Return dt
            Else
                Return Nothing
            End If
        End Function
        ''' <summary>
        ''' Tries to read a list of quote values from a System.Data.DataTable
        ''' </summary>
        ''' <param name="table">The System.Data.DataTable with the quote values</param>
        ''' <returns>The converted quote values or Nothing</returns>
        ''' <remarks></remarks>
        Public Function ToQuotesBaseData(ByVal table As System.Data.DataTable) As FeedItem()
            Dim lst As New List(Of FeedItem)

            If (table.Columns.Contains(RssHelper.NameFeedAuthor) AndAlso table.Columns(RssHelper.NameFeedAuthor).DataType Is GetType(String)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedCategory_Domain) AndAlso table.Columns(RssHelper.NameFeedCategory_Domain).DataType Is GetType(Uri)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedCategory_Name) AndAlso table.Columns(RssHelper.NameFeedCategory_Name).DataType Is GetType(String)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedComments) AndAlso table.Columns(RssHelper.NameFeedComments).DataType Is GetType(Uri)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedDescription) AndAlso table.Columns(RssHelper.NameFeedDescription).DataType Is GetType(String)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedEnclosure_Length) AndAlso table.Columns(RssHelper.NameFeedEnclosure_Length).DataType Is GetType(Long)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedEnclosure_Type) AndAlso table.Columns(RssHelper.NameFeedEnclosure_Type).DataType Is GetType(String)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedEnclosure_Url) AndAlso table.Columns(RssHelper.NameFeedEnclosure_Url).DataType Is GetType(Uri)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedGuid) AndAlso table.Columns(RssHelper.NameFeedGuid).DataType Is GetType(String)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedGuid_PermaLink) AndAlso table.Columns(RssHelper.NameFeedGuid_PermaLink).DataType Is GetType(Boolean)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedInsertDate) AndAlso table.Columns(RssHelper.NameFeedInsertDate).DataType Is GetType(Date)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedLink) AndAlso table.Columns(RssHelper.NameFeedLink).DataType Is GetType(Uri)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedPubDate) AndAlso table.Columns(RssHelper.NameFeedPubDate).DataType Is GetType(Date)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedSource_Title) AndAlso table.Columns(RssHelper.NameFeedSource_Title).DataType Is GetType(String)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedSource_Url) AndAlso table.Columns(RssHelper.NameFeedSource_Url).DataType Is GetType(Uri)) AndAlso _
               (table.Columns.Contains(RssHelper.NameFeedTitle) AndAlso table.Columns(RssHelper.NameFeedTitle).DataType Is GetType(String)) Then
                For Each row As System.Data.DataRow In table.Rows
                    Dim qd As New FeedItem
                    qd.Author = Convert.ToString(row(RssHelper.NameFeedAuthor))
                    If row(RssHelper.NameFeedCategory_Domain) IsNot Nothing Or row(RssHelper.NameFeedCategory_Name) IsNot Nothing Then
                        qd.Category = New Category
                        If row(RssHelper.NameFeedCategory_Domain) IsNot Nothing Then qd.Category.Domain = DirectCast(row(RssHelper.NameFeedCategory_Domain), Uri)
                        If row(RssHelper.NameFeedCategory_Name) IsNot Nothing Then qd.Category.Name = Convert.ToString(row(RssHelper.NameFeedCategory_Name))
                    End If

                    If row(RssHelper.NameFeedComments) IsNot Nothing Then qd.Comments = DirectCast(row(RssHelper.NameFeedComments), Uri)
                    If row(RssHelper.NameFeedDescription) IsNot Nothing Then qd.Description = Convert.ToString(row(RssHelper.NameFeedDescription))
                    If row(RssHelper.NameFeedEnclosure_Length) IsNot Nothing And row(RssHelper.NameFeedEnclosure_Type) IsNot Nothing And row(RssHelper.NameFeedEnclosure_Url) IsNot Nothing Then
                        qd.Enclosure = New Enclosure
                        If row(RssHelper.NameFeedEnclosure_Length) IsNot Nothing Then qd.Enclosure.Length = Convert.ToInt64(row(RssHelper.NameFeedEnclosure_Length))
                        If row(RssHelper.NameFeedEnclosure_Type) IsNot Nothing Then qd.Enclosure.Type = Convert.ToString(row(RssHelper.NameFeedEnclosure_Type))
                        If row(RssHelper.NameFeedEnclosure_Url) IsNot Nothing Then qd.Enclosure.Url = DirectCast(row(RssHelper.NameFeedEnclosure_Url), Uri)
                    End If
                    If row(RssHelper.NameFeedGuid) IsNot Nothing And row(RssHelper.NameFeedGuid_PermaLink) IsNot Nothing Then
                        qd.GUID = New GUID
                        If row(RssHelper.NameFeedGuid) IsNot Nothing Then qd.Author = Convert.ToString(row(RssHelper.NameFeedGuid))
                        If row(RssHelper.NameFeedGuid_PermaLink) IsNot Nothing Then qd.Author = Convert.ToString(row(RssHelper.NameFeedGuid_PermaLink))
                    End If

                    If row(RssHelper.NameFeedInsertDate) IsNot Nothing Then qd.InsertDate = Convert.ToDateTime(row(RssHelper.NameFeedInsertDate))
                    If row(RssHelper.NameFeedLink) IsNot Nothing Then qd.Link = DirectCast(row(RssHelper.NameFeedLink), Uri)
                    If row(RssHelper.NameFeedPubDate) IsNot Nothing Then qd.Author = Convert.ToString(row(RssHelper.NameFeedPubDate))
                    If row(RssHelper.NameFeedSource_Title) IsNot Nothing Then qd.Author = Convert.ToString(row(RssHelper.NameFeedSource_Title))
                    If row(RssHelper.NameFeedSource_Title) IsNot Nothing Then qd.Author = Convert.ToString(row(RssHelper.NameFeedSource_Title))
                    If row(RssHelper.NameFeedSource_Url) IsNot Nothing Then qd.Author = Convert.ToString(row(RssHelper.NameFeedSource_Url))
                    If row(RssHelper.NameFeedTitle) IsNot Nothing Then qd.Author = Convert.ToString(row(RssHelper.NameFeedTitle))


                    lst.Add(qd)
                Next
            End If
            Return lst.ToArray
        End Function



    End Class


End Namespace
